package com.dddframework.web.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
@ConfigurationProperties(prefix = "base-web")
public class BaseWebProperties {
    private Log log = new Log();
    private Mvc mvc = new Mvc();
    private Auth auth = new Auth();

    @Data
    public static class Log {
        private String includes = "/**";
        private String excludes = "/error";
    }

    @Data
    public static class Mvc {
        private String timePattern = "yyyy-MM-dd HH:mm:ss";
    }

    @Data
    public static class Auth {
        /**
         * Bearer访问令牌列表
         */
        private List<String> bearerTokens = new ArrayList<>();
        /**
         * access-token在Redis的缓存前缀
         */
        private String tokenKeyCachePrefix = "base-web::auth::accessToken::";
        /**
         * access-token的有效期
         */
        private Long tokenExpire = 48L;
        /**
         * 授权应用列表
         */
        private List<App> apps = new ArrayList<>();

        public App getApp(String appId, String appSecret) {
            for (App app : apps) {
                if (Objects.equals(app.getAppId(), appId) && Objects.equals(app.getAppSecret(), appSecret)) return app;
            }
            return null;
        }

        @Data
        public static class App {
            /**
             * 授权应用名称
             */
            private String name;
            /**
             * 授权应用描述
             */
            private String description;
            /**
             * 授权应用ID
             */
            private String appId;
            /**
             * 授权应用密钥
             */
            private String appSecret;
            /**
             * 授权租户ID
             */
            private String tenantId;
        }
    }
}
